package cn.wk.libs4a.view;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import cn.wk.libs4a.R;
import cn.wk.libs4a.WKBaseActivity;

/**
 * 一个跟JS交互的Webview
 */
public class WKWebForJSAct extends WKBaseActivity {

	private String url = "";
	private TextView msgView = null;

	private Handler handler = new Handler();

	@Override
	public void loadXml() {
		setContentView(R.layout.wk_act_web_for_js);
	}

	WebView webView;

	@SuppressLint("JavascriptInterface")
	@Override
	public void init() {

		url = getIntent().getStringExtra("url");
		msgView = (TextView) findViewById(R.id.msg);

		webView = (WebView) super.findViewById(R.id.ad_webview);
		webView.setWebViewClient(new WebViewClient() {
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				// 设置点击网页里面的链接还是在当前的webview里跳转
				view.loadUrl(url);
				return true;
			}
		});
		// webView.loadUrl(url.startsWith("http") ? url : "http://" + url);
		getHeader().setClickListener(null, null, new OnClickListener() {
			public void onClick(View arg0) {
				String which = "playaudio";
				String args = "10";
				webView.loadUrl("javascript:runJSBack(" + "'" + which + "','22')");
			}
		});
		// 启用javascript
		webView.getSettings().setJavaScriptEnabled(true);

		webView.loadUrl("file:///android_asset/test.html");
		webView.addJavascriptInterface(this, "test");
		// webView.addJavascriptInterface(object, name)

		OnClickListener btnClickListener = new Button.OnClickListener() {
			public void onClick(View v) {
				// 无参数调用
				webView.loadUrl("javascript:javacalljs()");
				// 传递参数调用
				webView.loadUrl("javascript:javacalljswithargs("
						+ "'hello world'" + ")");
			}
		};
	}

	public void startFunction() {
		Toast.makeText(this, "js调用了java函数", Toast.LENGTH_SHORT).show();
		runOnUiThread(new Runnable() {
			public void run() {
				msgView.setText(msgView.getText() + "\njs调用了java函数");
			}
		});
	}

	public void startFunction(final String str) {
		Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
		runOnUiThread(new Runnable() {
			public void run() {
				msgView.setText(msgView.getText() + "\njs调用了java函数传递参数：" + str);
			}
		});
	}

	/**
	 * JS调用原生方法
	 * 
	 * @param type
	 *            模块类型，1音频，2视频，3图文，4测试，5初次预约，6再次预约
	 * @param id
	 *            相关id，如测试模块的测试id，如图文信息的资讯id
	 * @param url
	 *            资源链接
	 * @param img
	 *            介绍图片
	 * @param info
	 *            介绍文本
	 * @param count
	 *            播放次数
	 * @param time
	 *            资源时长
	 */
	// @JavascriptInterface
	public void runJS(String type, String id, String url, String img,
			String info, int count, int time) {
		handler.postDelayed(new Runnable() {
			public void run() {
				runJSBack("", "args");
			}
		}, 1000);
	}

	/**
	 * JSBack,给JS返回值
	 * 
	 * @param type
	 *            模块类型，1音频，2视频，3图文，4测试，5初次预约，6再次预约
	 * @param args
	 *            返回的参数，type＝12播放完，3显示完，4测试结果，56预约信息
	 */
	@JavascriptInterface
	public void runJSBack(final String type, final String args) {
		handler.post(new Runnable() {
			public void run() {
				webView.loadUrl("javascript:runJSBack(" + "'" + type + "','"
						+ args + "')");
			}
		});
	}

	@Override
	public void setData() {
	}

}
